/*
 *@本JS功能：班级管理
 *@本js作者：杨锡君
 *@编写时间：2017年10月31日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var n = 0;
    var $departNames = $("#departNames");
    var $tableBox = $(".tableBox");
    var $searchBtn = $(".searchBtn");
    var $exportEvent = $(".exportBtn");
    var $exportCancle = $(".exportCancle");
    var $fileName = $(".fileName");
    var returnClassName = 0;
    var th = "<tr>" +
        "<th >学号</th>" +
        "<th >课程</th>" +
        "<th >姓名</th>" +
        "<th >实验总时长</th>" +
        "<th >实验报告数</th>" +
        "<th >实验笔记数</th>" +
        "<th >提问问题数</th>" +
        "<th >回答问题数</th>" +
        "<th >提交作业数</th>" +
        "<th >平时成绩换算</th>" +
        "<th >考试成绩换算</th>" +
        "<th >总成绩</th>" +
        "<th >操作</th>" +
        "</tr>";
    function tabelModel(opt) {
        return "<tr data-num=\"0\">" +
            "<td  title=" + opt.stuorteaId + ">" + opt.stuorteaId + "</td>" +
            "<td  title=" + opt.courseName + ">" + opt.courseName + "</td>" +
            "<td  title=" + opt.userName + ">" + opt.userName + "</td>" +
            "<td>" +  Number((opt.sumtime/3600).toFixed(2)) + "h</td>" +
            "<td>" + opt.exprecount + "</td>" +
            "<td>" + opt.exprenotecount + "</td>" +
            "<td>" + opt.problemcount + "</td>" +
            "<td>" + opt.answercount + "</td>" +
            "<td>" + opt.taskcount + "</td>" +
            "<td>" + opt.preforSource + "分</td>" +
            "<td>" + opt.examSource + "分</td>" +
            "<td >" + opt.sumSource + "分</td>" +
            "<td >" +
            "<a href=\"javascript:;\" data-name="+ opt.userName +" data-coursename="+ opt.courseName +" data-courseid="+ opt.courseId +" data-stuorteaid="+ opt.stuorteaId +" data-classname="+ opt.className +" data-classid=" + opt.classId + "  class=\"detailBtn\">详情</a>" +
            "</td>" +
            "</tr>";
    }
    var classMana = {
        init: function() {
            this.bindEvent();
            maxLengthLimit.init("publicText");
        },
        bindEvent: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10
            };
            if(result.paramData!=undefined){
                json.classId = Number(result.paramData.classId);
                json.courseId = Number(result.paramData.courseId);
            }
            self.tabelList(json);
            $searchBtn.off("click").on("click", function() {
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10
                };
                var className = $("#className").val();
                returnClassName = className;
                if(className != 0) {
                    jsonStr.classId = Number(className);
                }
                self.tabelList(jsonStr);
            });
        },
        tabelList: function(jsons) {
            var self = this;
            var json = JSON.stringify(jsons);
            loading1.show();
            $.ajax({
                type: "post",
                url: "getStuStatisticsByTeaId",
                data: {
                    "jsonStr": json
                },
                async: true,
                success: function(msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    $tableBox.empty();
                    if(msg.result == "success"){
                        if(msg.stuStatisticsMap.finalList!=null){
                            if(msg.stuStatisticsMap.finalList.length != 0) {
                                //列表
                                var tabel = "";
                                $.map(msg.stuStatisticsMap.finalList, function(opt) {
                                    tabel += tabelModel(opt);
                                });
                                $tableBox.append(th + tabel);
                                self.skipEvent();

                            }
                        } else {
                            var trImg = "<tr ><td colspan='13'> <img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        // $("#className").append()

                        // if(n == 0){
                        //     n++;
                        //     if(msg.stuStatisticsMap.classList.length!=0){
                        //         $.map(msg.stuStatisticsMap.classList,function (opt) {
                        //             $("#className").append("<option value="+ opt.classId +">"+ opt.className +"</option>")
                        //         });
                        //         if(result.paramData!=undefined){
                        //             $("#className").val(result.paramData.classId);
                        //         }
                        //     }
                        // }

                        var sumpage = msg.stuStatisticsMap.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getStuStatisticsByTeaId",
                                    data: {
                                        "jsonStr":JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        msg = JSON.parse(msg);
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        $.map(msg.stuStatisticsMap.finalList, function(opt) {
                                            tabel += tabelModel(opt);
                                        });
                                        $tableBox.append(th + tabel);
                                        self.skipEvent();
                                    }
                                });
                            }
                        });
                    }else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }


                }
            });

        },
        skipEvent:function(){
            $(".detailBtn").off("click").click(function(){
                var json = JSON.stringify({
                    "pageName":"stuStatisticsDetail",
                    "paramData":{
                        "courseId":$(this).attr("data-courseid"),
                        "pageName":"stuStatistics",
                        "className":$(this).attr("data-classname"),
                        "classId":$(this).attr("data-classid"),
                        "stuorteaId":$(this).attr("data-stuorteaid"),
                        "userName":$(this).attr("data-name"),
                        "courseName":$(this).attr("data-coursename"),
                        "returnClassName":returnClassName
                    }
                });
                window.location.href = "skipPage?jsonStr="+json;
            });
        }
    };
    classMana.init();
});